package com.desay.pangoo.itmt.config;

import java.io.File;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import com.desay.pangoo.itmt.entity.MailLogEntity;
import com.desay.pangoo.itmt.repository.MailLogEntityRepository;

import lombok.extern.slf4j.Slf4j;

/**
 * 发送邮件服务
 * 
 * @author uidq1163
 *
 */
@Service
@Slf4j
public class MailSenderService {
	@Autowired
	private JavaMailSender mailSender;
	@Value("${mail.fromMail.addr}")
	private String from;
	@Autowired
	private MailLogEntityRepository mailLogEntityRepository;

	public void sendMail(String title, String message, String mail, String[] cc) throws MessagingException {
		MailLogEntity mailLog = new MailLogEntity();
		try {
			MimeMessage mimeMessage = mailSender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
			helper.setFrom(from);
			helper.setTo(mail);
			helper.setSubject(title);
			// 第二个参数true，表示text的内容为html
			helper.setText(message, true);
			if (null != cc && cc.length > 0) {
				helper.setCc(cc);
			}
			mailSender.send(mimeMessage);
			mailLog.setStatus(true);
		} catch (Exception e) {
			log.error("{}", e);
			mailLog.setStatus(false);
		}
		// 记录邮件发送成功日志
		mailLog.setDetails(message);
		mailLog.setTitle(title);
		mailLog.setFrom(from);
		mailLog.setTo(mail);
		mailLog.setCc(Arrays.toString(cc));
		mailLogEntityRepository.save(mailLog);
	}

	public void sendMail(String title, String message, String[] mails, String[] cc) {
		MailLogEntity mailLog = new MailLogEntity();
		try {
			MimeMessage mimeMessage = mailSender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
			helper.setFrom(from);
			helper.setTo(mails);
			helper.setSubject(title);
			// 第二个参数true，表示text的内容为html
			helper.setText(message, true);
			if (null != cc && cc.length > 0) {
				helper.setCc(cc);
			}
			mailSender.send(mimeMessage);
			mailLog.setStatus(true);
		} catch (Exception e) {
			log.error("{}", e);
			mailLog.setStatus(false);
		}
		// 记录邮件发送成功日志
		mailLog.setDetails(message);
		mailLog.setTitle(title);
		mailLog.setFrom(from);
		mailLog.setTo(Arrays.toString(mails));
		mailLog.setCc(Arrays.toString(cc));
		mailLog.setStatus(true);
		mailLogEntityRepository.save(mailLog);
	}

	public void sendImageMail(String[] to, String subject, String content, Map<String, File> files, String[] cc) {
		// 创建message
		// 发送邮件
		MailLogEntity mailLog = new MailLogEntity();
		try {
			MimeMessage mimeMessage = mailSender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
			// 发件人
			helper.setFrom(from);
			// 收件人
			helper.setTo(to);
			// 标题
			helper.setSubject(subject);
			// true指的是html邮件，false指的是普通文本
			helper.setText(content, true);
			if (null != cc && cc.length > 0) {
				helper.setCc(cc);
			}
			// 添加图片
			if (null != files) {
				Iterator<Entry<String, File>> entries = files.entrySet().iterator();
				while (entries.hasNext()) {
					Entry<String, File> entry = entries.next();
					String key = entry.getKey();
					File file = entry.getValue();
					FileSystemResource fileResource = new FileSystemResource(file);
					helper.addInline(key, fileResource);
				}
			}
			mailSender.send(mimeMessage);
			mailLog.setStatus(true);
		} catch (Exception e) {
			log.error("{}", e);
			mailLog.setStatus(false);
		}
		// 记录邮件发送成功日志
		mailLog.setDetails(content);
		mailLog.setTitle(subject);
		mailLog.setFrom(from);
		mailLog.setTo(Arrays.toString(to));
		mailLog.setCc(Arrays.toString(cc));
		mailLogEntityRepository.save(mailLog);
	}
}
